//
//  STDropPictureViewController.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/30.
//

import UIKit

class STDropPictureViewController: GPImagePickerController {
    
    var orderId: Int = 0
    var fileId: String = ""
    let requestApi = GPRequestApi.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavBarTitle("上传落重箱照片")
        addTopBGView()
        addRightBarButtonItem("拍照指引")
        
        let bgView = UIView.init()
        bgView.backgroundColor = UIColor.white
        bgView.layer.cornerRadius = 12
        view.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalTo(40)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(kScreenWidth - 32)
        }
        
        let img_h = 256 * (kScreenWidth - 32) / 312
        bgView.addSubview(imgBtn)
        imgBtn.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(img_h)
        }
        
        let btn = UIButton.init(type: .custom)
        btn.setTitle("点击上传", for: .normal)
        btn.layer.cornerRadius = 5
        btn.backgroundColor = COLOR_HEX(hexString: "#3686FF", alpha: 1)
        btn.setTitleColor(UIColor.white, for: UIControl.State.normal)
        bgView.addSubview(btn)
        btn.snp.makeConstraints { (make) in
            make.top.equalTo(imgBtn.snp.bottom).offset(8)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(40)
        }
        
        imgBtn.addTarget(self, action: #selector(uploadPicture), for: UIControl.Event.touchUpInside)
        btn.addTarget(self, action: #selector(handleSubmitAction), for: UIControl.Event.touchUpInside)
    }
    
    override func rightBarButtonAction() {
        let vc = STGuidanceViewController.init()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    override func handleImagePickerResult (_ image: UIImage, _ filePath: String) {
        imgBtn.setImage(image, for: .normal)
        requestApi.uploadrSinglePic(image) { (isSuccess, response, error) in
            if(isSuccess) {
                let responseData = response as! Dictionary<String, Any>
                let data = responseData["data"] as! Dictionary<String, String>
                self.fileId = data["fileId"]!
            }
        }
    }
    
    @objc func uploadPicture() {
        showPickPictrueSheet()
    }
    
    @objc func handleSubmitAction() {
        let files: Array<Dictionary<String, Any>> = [["fileId": fileId, "num": 1]]
        self.showLoading("正在加载中...")
        let param = [ "orderId": orderId, "step": 3, "files": files ] as [String : Any]
        requestApi.submitOrder(param) { (isSuccess, response, error) in
            self.hideLoading()
            if(isSuccess) {
                self.toastInfo("上传成功")
                let vc: STOrderActionSuccessViewController = STOrderActionSuccessViewController.init()
                vc.status = 9
                vc.orderType = "0"
                self.navigationController?.pushViewController(vc, animated: true)
            } else {
                self.toastError(error!.1)
            }
        }
    }
    
    lazy var imgBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setImage(UIImage.init(named: "icon_shangchuan"), for: .normal)
        btn.layer.cornerRadius = 12
        btn.layer.masksToBounds = true
        return btn
    }()
}
